<?php
declare (strict_types=1);

namespace app\admin\model\system;

use app\common\helper\util\TimeFormat;
use app\common\model\BaseModel;

/**
 * 定义管理员系统消息数据模型
 */
class Message extends BaseModel
{
    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'system_message';

    /**
     * 设置自动写入时间
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 时间显示格式
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     * 定义表主键
     *
     * @var string
     */
    protected $pk = 'id';

    protected $append = ['format_time'];

    /**
     * 全局查询范围
     *
     * @var [type]
     */
    protected $globalScope = ['user'];

    /**
     * 友好的时间格式显示
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-23
     *
     * @param    [type]                         $value [description]
     * @param    [type]                         $data  [description]
     * @return   [type]                                [description]
     */
    public function getFormatTimeAttr($value, $data)
    {
        $time = new TimeFormat();

        return $time->formate($data['create_time']);
    }

    /**
     * 当前登录用户
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-23
     *
     * @param    [type]                         $query [description]
     * @return   [type]                                [description]
     */
    public function scopeUser($query)
    {
        return $query->where('admin_user_id', request()->admin_user_id);
    }
}
